﻿using Common.Models;
using Core.Entities.SM;
using Infrastructure.Services;
using Infrastructure.Services.SM;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace WebZHX.Controllers.SM
{
    /// <summary>
    /// 库存台账控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class StockController : BaseController<Stock, string>
    {
        private readonly StockService _stockService;

        public StockController(
            BaseService<Stock, string> service,
            StockService stockService)
            : base(service)
        {
            _stockService = stockService;
        }

        /// <summary>
        /// 根据物料编码查询库存
        /// </summary>
        /// <param name="materCode">物料编码</param>
        /// <returns>库存信息列表</returns>
        [HttpGet("mater/{materCode}")]
        public async Task<ActionResult<IEnumerable<Stock>>> GetByMaterCode(string materCode)
        {
            var result = await _stockService.GetByMaterCodeAsync(materCode);
            return Ok(result);
        }

        /// <summary>
        /// 根据批次号查询库存
        /// </summary>
        /// <param name="batchCode">批次号</param>
        /// <returns>库存信息列表</returns>
        [HttpGet("batch/{batchCode}")]
        public async Task<ActionResult<IEnumerable<Stock>>> GetByBatchCode(string batchCode)
        {
            var result = await _stockService.GetByBatchCodeAsync(batchCode);
            return Ok(result);
        }

        /// <summary>
        /// 根据仓库ID查询库存
        /// </summary>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns>库存信息列表</returns>
        [HttpGet("warehouse/{warehouseId}")]
        public async Task<ActionResult<IEnumerable<Stock>>> GetByWarehouseId(string warehouseId)
        {
            var result = await _stockService.GetByWarehouseIdAsync(warehouseId);
            return Ok(result);
        }

        /// <summary>
        /// 根据筒仓编码查询库存
        /// </summary>
        /// <param name="siloCode">筒仓编码</param>
        /// <returns>库存信息列表</returns>
        [HttpGet("silo/{siloCode}")]
        public async Task<ActionResult<IEnumerable<Stock>>> GetBySiloCode(string siloCode)
        {
            var result = await _stockService.GetBySiloCodeAsync(siloCode);
            return Ok(result);
        }

        public class StockQueryDto
        {
            /// <summary>
            /// 分页数据
            /// </summary>
            public QueryFilter Filter { get; set; }

            /// <summary>
            /// 库存查询条件
            /// </summary>
            public Stock Stock { get; set; }
        }

        /// <summary>
        /// 高级分页查询库存
        /// </summary>
        /// <param name="dto">查询参数</param>
        /// <returns>分页结果</returns>
        [HttpPost("stock_page")]
        public async Task<ActionResult<PagedResult<Stock>>> AdvancedPagedQuery([FromBody] StockQueryDto dto)
        {
            var result = await _stockService.AdvancedPagedQueryAsync(dto.Filter, dto.Stock);
            return Ok(result);
        }

        #region 新增、修改、删除等业务逻辑

        /// <summary>
        /// 保存库存信息
        /// </summary>
        /// <param name="model">库存信息</param>
        /// <returns>创建或更新的库存记录</returns>
        [HttpPost("save-stock")]
        public async Task<ActionResult<Stock>> SaveStock([FromBody] Stock model)
        {
            // 验证必填字段
            if (string.IsNullOrEmpty(model.MaterCode))
            {
                return BadRequest("物料编码不能为空");
            }

            if (string.IsNullOrEmpty(model.MaterName))
            {
                return BadRequest("物料名称不能为空");
            }

            if (string.IsNullOrEmpty(model.Id))
            {
                // 新增
                model.CreateTime = DateTime.Now;
                model.Id = Guid.NewGuid().ToString();
                var result = await _stockService.StockADD(model);
                return CreatedAtAction(nameof(Get), new { id = result }, result);
            }
            else
            {
                // 修改
                model.OutTime = DateTime.Now;
                var result = await _stockService.StockADD(model);
                return CreatedAtAction(nameof(Get), new { id = result }, result);
            }
        }


        #endregion
    }
}